package com.xinsoft.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.ScmPurchaseNoticeItemParam;
import com.xinsoft.entity.dto.ScmPurchaseNoticePageParam;
import com.xinsoft.entity.dto.ScmPurchasePageParam;
import com.xinsoft.entity.dto.ScmPurchasePlanItemParam;
import com.xinsoft.entity.po.ScmPurchaseNotice;
import com.xinsoft.entity.po.ScmPurchaseNoticeItem;
import com.xinsoft.entity.vo.*;
import com.xinsoft.service.ScmPurchaseNoticeItemService;
import com.xinsoft.service.ScmPurchaseNoticeService;
import com.xinsoft.mapper.ScmPurchaseNoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
* @author 13370
* @description 针对表【scm_purchase_notice(收料通知)】的数据库操作Service实现
* @createDate 2023-07-20 20:22:01
*/
@Service
public class ScmPurchaseNoticeServiceImpl extends ServiceImpl<ScmPurchaseNoticeMapper, ScmPurchaseNotice>
    implements ScmPurchaseNoticeService{

    @Autowired
    private ScmPurchaseNoticeItemService scmPurchaseNoticeItemService;

    @Override
    public Response<?> addOrUpdate(ScmPurchaseNotice scmPurchaseDto) {
        return null;
    }

    @Override
    public Response<?> findPageListByParam(ScmPurchaseNoticePageParam purchaseNoticePageParam) {
        //分页
        Page<ScmPurchaseNoticePageVo> page = new Page<>(purchaseNoticePageParam.getCurrentPage(), purchaseNoticePageParam.getPageSize());
        //查询分页
        IPage<ScmPurchaseNoticePageVo> pageList = this.baseMapper.findListByParam(page, purchaseNoticePageParam);
        if (ObjectUtil.isNotEmpty(pageList.getRecords())){
            pageList.getRecords().forEach(item ->{
                item.setViceNum((ObjectUtil.isEmpty(item.getNeedNum()) || item.getNeedNum().equals(BigDecimal.ZERO) || ObjectUtil.isEmpty(item.getConvCoefficient())) ?
                        BigDecimal.ZERO : item.getNeedNum().multiply(item.getConvCoefficient()));
            });
        }
        return Response.succeed(pageList);
    }

    @Override
    public List<ScmPurchaseNoticePageVo> findListByParam(ScmPurchaseNoticePageParam purchaseNoticePageParam) {
        List<ScmPurchaseNoticePageVo> list = this.baseMapper.findListByParam(purchaseNoticePageParam);
        return list;
    }

    @Override
    public Response<?> getNotice(Integer id) {
        ScmPurchaseNoticeVo scmPurchaseNotice = this.baseMapper.getScmPurchaseNotice(id);
        ScmPurchaseNoticeItemParam noticeItemParam = new ScmPurchaseNoticeItemParam();
        noticeItemParam.setPurchaseNoticeId(id);
        List<ScmPurchaseNoticeItemVo>  purchaseNoticeItemVos = scmPurchaseNoticeItemService.listNoticeItem(noticeItemParam);
        purchaseNoticeItemVos.forEach(item ->{
            item.setViceNum((ObjectUtil.isEmpty(item.getNeedNum()) || item.getNeedNum().equals(BigDecimal.ZERO) || ObjectUtil.isEmpty(item.getConvCoefficient())) ?
                    BigDecimal.ZERO : item.getNeedNum().multiply(item.getConvCoefficient()));
        });
        scmPurchaseNotice.setItemList(purchaseNoticeItemVos);
        return Response.succeed(scmPurchaseNotice);
    }

}




